/**
* This file is part of tictionary.
*
* tictionary is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with dev.clipall.  If not, see <http://www.gnu.org/licenses/>.
*
* http://code.google.com/p/tictionary/
*/

package dicterator.dictTreeCreator;

import dicterator.Utils;
import dicterator.dict.Dict;
import dicterator.dict.Letter;
import dicterator.dict.LetterSet;

/**
 *
 * @author Erol Hira
 */
public class OffsetComputer {

    private Dict dict;

    private int offset;

    public OffsetComputer(Dict dict) {
        this.dict = dict;
    }

    private void calculateSelfOffsets(LetterSet letterSet){

        if(letterSet == null){
            return;
        }

        offset ++; // 1 byte for letter count in the letterSet

        for(int i = 0; i < letterSet.getLetterCount(); i++){

            Letter letter = letterSet.getLetter(i);

            letter.setSelfSetOffset(offset);

            if(letter.getChars() != null){
            //if(letter.getChildSet() == null){

                offset += 1 // count of n'th letter's chars
                        + letter.getChars().length() // letters
                        + 2; // mean offset
                        
            } else {

                offset += 1 // letter
                        + 2 // childset offset
                        + 2; // mean offset
            }           
        }

        for(int i = 0; i < letterSet.getLetterCount(); i++){

            Letter letter = letterSet.getLetter(i);

            LetterSet childSet = letter.getChildSet();
            calculateSelfOffsets(childSet);
        }
    }

    private void calculateChildOffsets(LetterSet letterSet){

        if(letterSet == null){
            return;
        }

        for(int i = 0; i < letterSet.getLetterCount(); i++){

            Letter letter = letterSet.getLetter(i);

            LetterSet childSet = letter.getChildSet();

            if(childSet == null || childSet.getLetterCount() == 0){
                letter.setChildSetOffset(-1);
            } else {

                //int letterOffset = letter.getSelfSetOffset();

                //int childSetOffset = childSet.getLetter(0).getSelfSetOffset() - letterOffset;
                int childSetOffset = childSet.getLetter(0).getSelfSetOffset() - 1;

                letter.setChildSetOffset(childSetOffset);
            }
        }


        for(int i = 0; i < letterSet.getLetterCount(); i++){

            Letter letter = letterSet.getLetter(i);

            LetterSet childSet = letter.getChildSet();
            calculateChildOffsets(childSet);
        }
    }

    private void calculateMeanOffsets(LetterSet letterSet){

        if(letterSet == null){
            return;
        }

        for(int i = 0; i < letterSet.getLetterCount(); i++){

            Letter letter = letterSet.getLetter(i);


            if(letter.getMean() == null){
                //System.out.println("mean null...");
            } else {
                letter.setMeanOffset(offset);
                offset += 1 + Utils.string2bytes(letter.getMean()).length; // short(mean length) + letter bytes
            }
        }

        for(int i = 0; i < letterSet.getLetterCount(); i++){

            Letter letter = letterSet.getLetter(i);

            LetterSet childSet = letter.getChildSet();
            calculateMeanOffsets(childSet);
        }
    }


    public void calculateOffsets(){

        LetterSet rootSet = dict.getRootSet();

        calculateSelfOffsets(rootSet);
        calculateChildOffsets(rootSet);
        calculateMeanOffsets(rootSet);

        System.out.println("byteCount:" + offset);
    }
}
